package com.lhj.myapplication.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lhj.myapplication.service.ArticleService;
import com.lhj.myapplication.utils.PageModel;
import com.lhj.myapplication.vo.ArticleViewModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @GetMapping("/articles")
    @ApiOperation("分页获取文章信息")
    public PageModel getArticles(Integer pageIndex, Integer pageSize,String name){
        if (pageIndex==null||pageIndex<=0){
            pageIndex=1;
        }
        if (pageSize==null||pageIndex<=0){
            pageSize=10;
        }
        Map param=new HashMap();
        param.put("name",name);
        PageHelper.startPage(pageIndex,pageSize);
        Page<ArticleViewModel> data=articleService.findByPaging(param);
        PageModel pageModel=new PageModel(data,pageIndex,pageSize,data.getTotal(),data.getPages());
        return pageModel;
    }
}
